﻿using Awsc.DataAcquisitionSys.Core.Common;
using Awsc.DataAcquisitionSys.Core.Model.Config;
using Awsc.DataAcquisitionSys.Core.Services.ConfigManagement;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace Awsc.DataAcquisitionSys.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ConfigController : ControllerBase
    {
        private readonly IOptionsMonitor<DeviceConfig> config;
        private readonly IDeviceConfigService deviceConfigService;

        public ConfigController(IDeviceConfigService deviceConfigService)
        {
            this.deviceConfigService = deviceConfigService;
        }
        [HttpGet("deviceConfig")]
        public async Task<DeviceConfig> GetDeviceConfig()
        {
            return deviceConfigService.GetDeviceConfig();
        }
        [HttpPost("deviceConfig/collectors")]
        public async Task<DeviceConfig> AddCollector([FromBody] CollectorConfig collectorConfig)
        {
            ArgumentAssert.NotNull(collectorConfig);
            await deviceConfigService.AddCollectorAsync(collectorConfig);

            return deviceConfigService.GetDeviceConfig();
        }

        [HttpPost("deviceConfig/collectors/{collectorName}")]
        public async Task<DeviceConfig> UpdateCollector([FromRoute] string collectorName, [FromBody] CollectorConfig collectorConfig)
        {
            ArgumentAssert.NotNullOrEmpty(collectorName);
            ArgumentAssert.NotNull(collectorConfig);
            await deviceConfigService.UpdateCollectorAsync(collectorName, collectorConfig);

            return deviceConfigService.GetDeviceConfig();
        }


        [HttpDelete("deviceConfig/collectors/{collectorName}")]
        public async Task<DeviceConfig> RemoveCollector([FromRoute] string collectorName)
        {
            ArgumentAssert.NotNullOrEmpty(collectorName);
            await deviceConfigService.RemoveCollectorAsync(collectorName);
            return deviceConfigService.GetDeviceConfig();
        }

        [HttpPost("deviceConfig/{collectorName}/registers")]
        public async Task<DeviceConfig> AddRegister([FromRoute] string collectorName, [FromBody] RegisterConfig registerConfig)
        {
            ArgumentAssert.NotNull(registerConfig);
            await deviceConfigService.AddRegisterAsync(collectorName, registerConfig);
            return deviceConfigService.GetDeviceConfig();
        }

        [HttpPost("deviceConfig/{collectorName}/registers/{registerName}")]
        public async Task<DeviceConfig> UpdateRegister([FromRoute] string collectorName, [FromRoute] string registerName, [FromBody] RegisterConfig registerConfig)
        {
            ArgumentAssert.NotNullOrEmpty(collectorName);
            ArgumentAssert.NotNullOrEmpty(registerName);
            ArgumentAssert.NotNull(registerConfig);

            await deviceConfigService.UpdateRegisterAsync(collectorName, registerName, registerConfig);
            return deviceConfigService.GetDeviceConfig();
        }


        [HttpDelete("deviceConfig/{collectorName}/registers/{registerName}")]
        public async Task<DeviceConfig> RemoveRegister([FromRoute] string collectorName, [FromRoute] string registerName)
        {
            ArgumentAssert.NotNullOrEmpty(registerName);
            await deviceConfigService.RemoveRegisterAsync(collectorName, registerName);
            return deviceConfigService.GetDeviceConfig();
        }


        [HttpPost("deviceConfig/{collectorName}/coils")]
        public async Task<DeviceConfig> AddCoil([FromRoute] string collectorName, [FromBody] CoilConfig coilConfig)
        {
            ArgumentAssert.NotNull(coilConfig);
            await deviceConfigService.AddCoilAsync(collectorName, coilConfig);
            return deviceConfigService.GetDeviceConfig();
        }

        [HttpPost("deviceConfig/{collectorName}/coils/{coilName}")]
        public async Task<DeviceConfig> UpdateCoil([FromRoute] string collectorName, [FromRoute] string coilName, [FromBody] CoilConfig coilConfig)
        {
            ArgumentAssert.NotNull(coilConfig);
            await deviceConfigService.UpdateCoilAsync(collectorName, coilName, coilConfig);
            return deviceConfigService.GetDeviceConfig();
        }

        [HttpDelete("deviceConfig/{collectorName}/coils/{coilName}")]
        public async Task<DeviceConfig> RemoveCoil([FromRoute] string collectorName, [FromRoute] string coilName)
        {
            ArgumentAssert.NotNullOrEmpty(coilName);
            await deviceConfigService.RemoveCoilAsync(collectorName, coilName);
            return deviceConfigService.GetDeviceConfig();
        }

        [HttpPost("deviceConfig/cameras")]
        public async Task<DeviceConfig> AddCamera(CameraConfig cameraConfig)
        {
            ArgumentAssert.NotNull(cameraConfig);
            await deviceConfigService.AddCameraAsync(cameraConfig);
            return deviceConfigService.GetDeviceConfig();
        }

        [HttpPost("deviceConfig/cameras/{cameraName}")]
        public async Task<DeviceConfig> UpdateCamera([FromRoute] string cameraName, CameraConfig cameraConfig)
        {
            ArgumentAssert.NotNull(cameraConfig);
            await deviceConfigService.UpdateCameraAsync(cameraName, cameraConfig);
            return deviceConfigService.GetDeviceConfig();
        }

        [HttpDelete("deviceConfig/cameras/{cameraName}")]
        public async Task<DeviceConfig> AddCamera([FromRoute] string cameraName)
        {
            ArgumentAssert.NotNullOrEmpty(cameraName);
            await deviceConfigService.RemoveCameraAsync(cameraName);
            return deviceConfigService.GetDeviceConfig();
        }


    }
}
